<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userFormRef" :model="userInfo" :rules="rules" label-width="220px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input
                  v-model="userInfo.username"
                  placeholder="请输入姓名"
                  class="inputW"
                  size="mini"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号">
                <el-input
                  v-model="userInfo.workNumber"
                  placeholder="请输入工号"
                  class="inputW"
                  size="mini"
                  disabled
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机号" prop="mobile">
                <el-input
                  :disabled="!!$route.params.id"
                  v-model="userInfo.mobile"
                  class="inputW"
                  size="mini"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" placeholder="请选择" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <selectTree v-model="userInfo.departmentId" class="inputW"></selectTree>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" placeholder="请选择" prop="formOfEmployment">
                <el-select v-model="userInfo.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1"></el-option>
                  <el-option label="非正式" :value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <!-- value-format="yyyy-MM-dd" -->
                <el-date-picker
                  v-model="userInfo.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="userInfo.correctionTime"
                  value-format="yyyy-MM-dd"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                 <imageUpload v-model="userInfo.staffPhoto"></imageUpload>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12"> </el-col>
          </el-row>
          <el-row type="flex">
            <el-col :span="12" style="margin-left: 220px">
              <el-button type="primary" @click="saveDate"> 保存更新 </el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import selectTree from './components/select-tree.vue'
import imageUpload from './components/image-upload.vue'
import { addEmployeeData, getEmployeeDetailData, editEmployeeDetailData } from '@/api/employee.js'
export default {
  components: {
    selectTree,
    imageUpload
  },
  name: 'detail',
  data() {
    return {
      userInfo: {
        username: '',
        workNumber: '', //工号
        mobile: '',
        departmentId: null, //部门id
        formOfEmployment: null, //聘用形式
        timeOfEntry: '', //入职时间
        correctionTime: '', //转正时间
        staffPhoto: '' //员工头像
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            //   pattern 正则表达式
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        departmentId: [{ required: true, message: '请输入部门', trigger: 'blur' }],
        formOfEmployment: [{ required: true, message: '请输入聘用形式', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '请输入入职时间', trigger: 'blur' }],
        correctionTime: [
          { required: true, message: '请输入转正时间', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (this.userInfo.timeOfEntry) {
                if (new Date(this.userInfo.timeOfEntry) > new Date(value)) {
                  callback(new Error('转正时间不能小于入职时间'))
                  return
                }
              }
              callback()
            }
          }
        ]
      }
    }
  },

  async created() {
    this.$route.params.id && this.getEmployeeDetail()
  },
  methods: {
    saveDate() {
      this.$refs.userFormRef.validate(async isOk => {
        if (isOk) {
          // console.log(this.userInfo)
          if (this.$route.params.id) {
            //编辑模式
            await editEmployeeDetailData(this.userInfo)
            this.$message.success('修改员工成功!')
          } else {
            //添加模式
            await addEmployeeData(this.userInfo)
            this.$message.success('添加员工成功')
          }
          this.$router.push('/employee')
        }
      })
    },
    async getEmployeeDetail() {
      // 获取员工详情数据，赋值给userInfo
      this.userInfo = await getEmployeeDetailData(this.$route.params.id)
    }
  }
}
</script>

<style scoped lang="scss">
.edit-form {
  background-color: #fff;
  padding: 20px;
  .inputW {
    width: 300px;
  }
}
</style>
